#include "sms.h"

#define MEDIALIB_LAYOUT_NAME "layout-medialib"
#define MEDIALIB_MAX_TAB 8
#define MEDIALIB_MAX_SITE 8
#define MEDIALIB_MAX_MENU 8

typedef struct _Medialib
{
	GooCanvasItem *bg;
	GooCanvasItem *tab_group;
	GooCanvasItem *site_group;
	GooCanvasItem *menu_group;
	
	Button *tab_bt[MEDIALIB_MAX_TAB];
	Button *site_bt[MEDIALIB_MAX_SITE];
	Button *menu_bt[MEDIALIB_MAX_MENU];
	
	FileList *filelist;
	int tab_bt_cur;
	int tab_bt_count;
	int site_bt_cur;
	int site_bt_count;
	int menu_bt_cur;
	int menu_bt_count;
	
	gboolean in_menu;
	gboolean in_list;
	
}Medialib;

//////////////////////////////////////////////////////////////////////
static char *site_path[]={"/home","/mnt","/etc"};

#if 0
static gboolean filelist_load_dir(Layout *layout,const char *dir,int type)
{
	Medialib *medialib=layout->private;
	char *load_dir;
	char buf[512];
	medialib->filelist->cur_dir;
	
	if(type==-1)
		type=medialib->filelist->type;
	if(!dir)
		load_dir=(char *)dir;
	else
		load_dir=medialib->filelist->cur_dir;
}
#endif
static gboolean medialib_load_site(Layout *layout,int i)
{
	Medialib *medialib=layout->private;
	SMS *sms=layout->sms;
	int type;
	
	switch (medialib->tab_bt_cur)
	{
		case 1:
			type=STYPE_ALL;
			break;
		case 2:
			type=STYPE_MOVIE;
			break;
		case 3:
			type=STYPE_MUSIC;
			break;
		case 4:
			type=STYPE_PIC;
			break;
		case 5:
			type=STYPE_GAMES;
			break;
	}
	
	if(filelist_load_dir(medialib->filelist,site_path[i],type))
	{
		g_object_set(medialib->site_group,"visibility",GOO_CANVAS_ITEM_HIDDEN,NULL);
		
		g_object_set(medialib->filelist->lv->cavas,"visibility",GOO_CANVAS_ITEM_VISIBLE,NULL);
		
		medialib->in_list=TRUE;
		
		return TRUE;
	}
	
	return FALSE;
}
static GooCanvasItem* medialib_load_button_group(xmlNode *a_node,Layout *layout)
{
	GooCanvasItem *group;
	GdkPixbuf *pixbuf;
	char *val,buf[256];
	gdouble x,y,w,h;
	
	SMS *sms=layout->sms;
	
	x=(gdouble)sms_get_xml_int(a_node,"x",0);
	y=(gdouble)sms_get_xml_int(a_node,"y",0);
	w=(gdouble)sms_get_xml_int(a_node,"w",0);
	h=(gdouble)sms_get_xml_int(a_node,"h",0);
	group=goo_canvas_group_new(layout->cavas,"x",x,"y",y,"width",w,"height",h,NULL);
	
	val = xmlGetProp (a_node,(xmlChar*)"pic");
	if(val)
	{
		sprintf(buf,"%s/%s/layout/%s/%s",SMS_RESOURCE_DIR,sms->theme->name,MEDIALIB_LAYOUT_NAME,val);
		pixbuf=sms_load_pic(buf,w,h);
		if(pixbuf)
			goo_canvas_image_new (group, pixbuf, 0, 0, NULL);
	}
	
	return group;
}

static Button *medialib_creat_button(xmlNode *a_node,Layout *layout,GooCanvasItem*parent)
{
	Medialib *medialib=layout->private;
	SMS *sms=layout->sms;
	xmlNode *cur_node = NULL;
	GdkPixbuf *pixbuf,*icon_bg=NULL,*icon_fg=NULL;
	Button *bt;
	char buf[512],*val;
	int x,y,size,i=0;
	
	x=sms_get_xml_int(a_node,"x",0);
	y=sms_get_xml_int(a_node,"y",0);

	val = xmlGetProp (a_node,(xmlChar*)"text");
	if(!val)
		return NULL;

	bt=button_new_with_text(parent,x,y,sms->theme,val);
	if(!bt)
		return NULL;
	
	size=sms_get_xml_int(a_node,"icon_size",32);
	val = xmlGetProp (a_node,(xmlChar*)"icon_bg");
	if(val)
	{
		sprintf(buf,"%s/%s/layout/%s/%s",SMS_RESOURCE_DIR,sms->theme->name,MEDIALIB_LAYOUT_NAME,val);
		icon_bg=sms_load_pic(buf,size,size);
	}
	val = xmlGetProp (a_node,(xmlChar*)"icon_fg");
	if(val)
	{
		sprintf(buf,"%s/%s/layout/%s/%s",SMS_RESOURCE_DIR,sms->theme->name,MEDIALIB_LAYOUT_NAME,val);
		icon_fg=sms_load_pic(buf,size,size);
	}
	button_set_icon(bt,icon_fg,icon_bg);
	return bt;
}

static gboolean medialib_site_button_clicked(Button *bt,gpointer data)
{
	int i;
	Layout *layout=data;
	Medialib *medialib=layout->private;
	SMS *sms=layout->sms;
	for(i=0;i<medialib->site_bt_count;i++)
	{
		if(medialib->site_bt[i]==bt)
			break;
	}
	if(i<medialib->site_bt_count)
	{
		printf("id=%d\n",i);
		medialib_load_site(layout,i);
		return TRUE;
	}
	
	return FALSE;
}
static gboolean medialib_load_site_button(xmlNode *a_node,Layout *layout)
{
	Medialib *medialib=layout->private;
	SMS *sms=layout->sms;
	xmlNode *cur_node = NULL;
	Button *bt;
	int i=0;
	
	if(!a_node)
		return FALSE;

	for (cur_node = a_node; cur_node; cur_node = cur_node->next) {
		if (cur_node->type == XML_ELEMENT_NODE) {
			if(strcmp(cur_node->name,"button")==0)
			{	
				bt=medialib_creat_button(cur_node,layout,medialib->site_group);
				if(!bt)
					continue;
				medialib->site_bt[i]=bt;
						button_set_func(bt,"clicked",medialib_site_button_clicked,layout);
				i++;
				if(i>=MEDIALIB_MAX_TAB)
					break;
			}
			
		}
	}
	if(i==0)
		return FALSE;
	button_set_focus(medialib->site_bt[0],TRUE);
	medialib->site_bt_cur=0;
	medialib->site_bt_count=i;
	return TRUE;
}

static gboolean medialib_load_menu_button(xmlNode *a_node,Layout *layout)
{
	return TRUE;
}

static gboolean medialib_load_tab_button(xmlNode *a_node,Layout *layout)
{
	Medialib *medialib=layout->private;
	SMS *sms=layout->sms;
	xmlNode *cur_node = NULL;
	Button *bt;
	int i=0;
	
	if(!a_node)
		return FALSE;

	for (cur_node = a_node; cur_node; cur_node = cur_node->next) {
		if (cur_node->type == XML_ELEMENT_NODE) {
			if(strcmp(cur_node->name,"button")==0)
			{	
				bt=medialib_creat_button(cur_node,layout,medialib->tab_group);
				if(!bt)
					continue;
				
				button_set_ground(bt,sms->theme->pic[THEME_PIC_TAB_FG],NULL);
				medialib->tab_bt[i]=bt;
				
				i++;
				if(i>=MEDIALIB_MAX_TAB)
					break;
			}
			
		}
	}
	if(i==0)
		return FALSE;
	button_set_focus(medialib->tab_bt[0],TRUE);
	medialib->tab_bt_cur=0;
	medialib->tab_bt_count=i;
	return TRUE;
}

////////////////////////////
//file list

static gboolean medialib_filelist_enter(Layout *layout)
{
	Medialib *medialib=layout->private;
	SMS *sms=layout->sms;
	
	Node *n=listview_get_cur_item(medialib->filelist->lv);
	SFile *sf=n->private;
	
	printf("n->node_buf=%s\n",n->node_buf);
	printf("sf->fullname=%s,sf->type=%d\n",sf->fullname,sf->type);
	if(sf->type==STYPE_DIR)
		filelist_load_dir(medialib->filelist,sf->fullname,-1);
	//else
	//do exec	
	
	return FALSE;
}

static FileList *medialib_filelist_new(xmlNode *a_node,Layout *layout)
{
	Medialib *medialib=layout->private;
	SMS *sms=layout->sms;
	xmlNode *cur_node = NULL;
	int x,y;
	FileList * fl=NULL;
	if(!a_node)
		return NULL;

	x=sms_get_xml_int(a_node,"x",0);
	y=sms_get_xml_int(a_node,"y",0);
	
	fl=filelist_new(layout->cavas,x,y,LISTVIEW_NONE,sms->theme);
	
	g_object_set(fl->lv,"visibility",GOO_CANVAS_ITEM_HIDDEN,NULL);
	return fl;
}

///////////////////////////////
static gboolean medialib_travel_xml(xmlNode *a_node,Layout *layout)
{
	Medialib *medialib=layout->private;
	SMS *sms=layout->sms;
	char *val;
	xmlNode *cur_node = NULL;
	
	for (cur_node = a_node; cur_node; cur_node = cur_node->next) {
		if (cur_node->type == XML_ELEMENT_NODE) {
			if(strcmp(cur_node->name,"button-group")==0)
			{
				val = xmlGetProp (cur_node,(xmlChar*)"id");
				if(!val)
					continue;
				if(strcmp(val,"tab")==0)
				{
					medialib->tab_group=medialib_load_button_group(cur_node,layout);
					medialib_load_tab_button(cur_node->children,layout);
				}
				else if(strcmp(val,"site")==0)
				{
					medialib->site_group=medialib_load_button_group(cur_node,layout);
					medialib_load_site_button(cur_node->children,layout);
				}
				else if(strcmp(val,"menu")==0)
				{
					medialib->menu_group=medialib_load_button_group(cur_node,layout);
					medialib_load_menu_button(cur_node->children,layout);
				}
				else if(strcmp(val,"listview")==0)
					medialib->filelist=medialib_filelist_new(cur_node,layout);
			}
		}
	}

	if(medialib->tab_bt_count<=0)//site_bt_count,menu_bt_count
		return FALSE;
	
	return TRUE;
}

static gboolean medialib_load_config(Layout *layout)
{
	Medialib *medialib=layout->private;
	SMS *sms=layout->sms;
	
	gboolean ret=FALSE;
	xmlDoc *doc = NULL;
	xmlNode *root_element = NULL;
	
	char conf[256];
	
	sprintf(conf,"%s/%s/layout/%s/layout.xml",SMS_RESOURCE_DIR,sms->theme->name,layout->name);
	
	doc = xmlReadFile(conf, NULL, 0);

	if (doc == NULL) {
		fprintf(stderr,"error: could not parse file %s\n", conf);
		return ret;
	}

	root_element = xmlDocGetRootElement(doc);
	
	if(root_element)
	{
		xmlNode *cur_node = NULL;
		for (cur_node = root_element; cur_node; cur_node = cur_node->next) {
			if (cur_node->type == XML_ELEMENT_NODE) {
				ret=TRUE;
				break;
			}
		}
		if(ret)
			ret=medialib_travel_xml(cur_node->children,layout);
	}

	{
		gdouble x,y;
		g_object_get(medialib->site_group,"x",&x,"y",&y,NULL);
		medialib->filelist=filelist_new(layout->cavas,(int)x ,(int)y ,LISTVIEW_NONE,sms->theme);
	}
	xmlFreeDoc(doc);
	xmlCleanupParser();
	return ret;
}

static void medialib_change_tab(Layout *layout,int move)
{
	Medialib *medialib=layout->private;
	
	if( medialib->tab_bt_cur+move<0 ||
		   medialib->tab_bt_cur+move >= medialib->tab_bt_count )
		return ;
	
	button_set_focus(medialib->tab_bt[medialib->tab_bt_cur],FALSE);
	medialib->tab_bt_cur=medialib->tab_bt_cur+move;
	button_set_focus(medialib->tab_bt[medialib->tab_bt_cur],TRUE);
		
	if(medialib->in_list)
	{
		//medialib_refresh();
	}
}

static void medialib_change_site(Layout *layout,int move)
{
	Medialib *medialib=layout->private;
	
	if( medialib->site_bt_cur+move<0 ||
		   medialib->site_bt_cur+move >= medialib->site_bt_count )
		return ;
	
	button_set_focus(medialib->site_bt[medialib->site_bt_cur],FALSE);
	medialib->site_bt_cur=medialib->site_bt_cur+move;
	button_set_focus(medialib->site_bt[medialib->site_bt_cur],TRUE);
}

//////////////////////////////////////////////////////////////////////

gboolean medialib_init(Layout *layout,gpointer data)
{
	printf("call %s\n",__FUNCTION__);
	SMS *sms=layout->sms;
	GdkPixbuf *pixbuf;
	Medialib *medialib=(Medialib *)g_malloc0(sizeof(Medialib)+1);
	
	layout->private=(void *)medialib;

	layout->cavas = goo_canvas_group_new(layout->parent,NULL);	

	pixbuf=sms->theme->pic[THEME_PIC_LAYOUT_BG];
	medialib->bg=goo_canvas_image_new(layout->cavas,pixbuf,0,0,NULL);
	
	if(!medialib_load_config(layout))
	{
		fprintf(stderr,"%s:load config err\n",__FUNCTION__);
		return FALSE;//exit(-1);
	}
	return TRUE;
}

gboolean medialib_clear(Layout *layout,gpointer data)
{
	
	return TRUE;
}

gboolean medialib_key_press(Layout * layout,GdkEventKey *event)
{
	Medialib *medialib=layout->private;
	
	if(event->keyval==GDK_Escape)
	{		
		sms_change_win_to(layout->sms,LAYOUT_WIN_MAIN);
		return TRUE;
	}
	else if(event->keyval==GDK_Left)
	{
		if(!medialib->in_menu)
			medialib_change_tab(layout,-1);
		return TRUE;
	}
	else if(event->keyval==GDK_Right)
	{
		if(!medialib->in_menu)
			medialib_change_tab(layout,1);
		return TRUE;
	}
	if(event->keyval==GDK_Up)
	{
		if(medialib->in_menu)
			printf("in menu up\n");
		else if(medialib->in_list)
			listview_translate(medialib->filelist->lv,-1);
		else
			medialib_change_site(layout,-1);
		return TRUE;
	}
	else if(event->keyval==GDK_Down)
	{
		if(medialib->in_menu)
			printf("in menu up\n");
		else if(medialib->in_list)
			listview_translate(medialib->filelist->lv,1);
		else
			medialib_change_site(layout,1);
		return TRUE;
	}
	else if(event->keyval==GDK_Return){
		if(medialib->in_menu)
			printf("in menu GDK_Return\n");
		else if(medialib->in_list)
			medialib_filelist_enter(layout);
		else
		{
			Button *bt;
			bt=medialib->site_bt[medialib->site_bt_cur];
			if(bt->clicked_func)
				bt->clicked_func(bt,layout);
		}
	}
	return FALSE;
}

Layout *medialib_layout_new(GooCanvasItem * parent,gpointer sms)
{
	Layout *layout=layout_new( parent,LAYOUT_TYPE_WIN,LAYOUT_WIN_MEDIALIB,
							   MEDIALIB_LAYOUT_NAME,sms);

	layout_set_func(layout,"init",medialib_init);
	layout_set_func(layout,"clear",medialib_clear);
	layout_set_func(layout,"key-press",medialib_key_press);
	printf("%s:layout=%p\n",__FUNCTION__,layout);
	return layout;
}
